<?php

class Admin_Model_Product extends Zend_Db_Table_Abstract {

    protected $_name = 'products';
    protected $_primary = 'id';
    protected $_dependentTables = array('Admin_Model_ProductFeatureDetail');
    protected $_referenceMap = array(
    		'category'=>array(
    				'columns' => array('categoryId'),
    				'refTableClass' => 'Admin_Model_Category',
    				'refColumns' => array('id'),
    				'onDelete' => self::CASCADE,
    				'onUpdate' => self::CASCADE,
    		)
    
    );

    public function getProduct($params = null, $options = null) {
    	if($options == null){
        	$select = $this->select()
			        	->setIntegrityCheck ( false )
			        	->from ( "{$this->_name} AS p" )
			        	->join ( 'category AS c', 'c.id = p.categoryId', array('c.titleVi AS CategoryName'))
        				->where('p.isDelete = 0')
        				->where('c.isDelete = 0');
    	}elseif($options['task'] == 'by-category'){
    		//$categoryId = $params['categoryId'];
    		$select = $this->select()
    						->setIntegrityCheck ( false )
				        	->from ( "{$this->_name} AS p" )
				        	->join ( 'category AS c', 'c.id = p.categoryId', array('c.titleVi AS CategoryName'))
	        				->where('p.isDelete = 0')
	        				->where('c.isDelete = 0')
    						->where('categoryId = ?', (int) $params);
    	}
    	
        $result = $this->fetchAll($select);
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }

    public function getProductOne($id) {
        $select = $this->select()
        		->where('isDelete = 0')
                ->where('id = ?', (int) $id);
        
        $result = $this->fetchRow($select); 
        if ($result) {
            return $result->toArray();
        } else {
            return null;
        }
    }
    

    public function addItem($info) {
    	try {
        	$id = $this->insert($info);

    		return $id;
		} catch (Exception $e) {
			throw $e;
		}
    }

    public function updateItem($id, $info) {
    	try {
        	$result = $this->update($info, "id=" . (int) $id);
	        return $result;
        } catch (Exception $e) {
        	throw $e;
        }
    }
    
    /**
     * remove update column isDeleted = 1
     *
     * @param string $params
     * @param string $options
     * @return boolean
     */
    public function removeItem($params = null, $options = null) {
    	try {
    		$data = array('isDelete' => 1);
    		if($options == null){
    			$result = $this->updateItem($params, $data);
    		}elseif($options['task'] == 'remove-select'){
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}elseif($options['task'] == 'remove-all'){
    			$params = $this->getCategorys();
    
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val['id'], $data);
    			}
    		}
    		 
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }
    
    /**
     * restore update column isDeleted = 0
     *
     * @param string $params
     * @param string $options
     * @return boolean
     */
    public function restoreItem($params = null, $options = null) {
    	try {
    		$data = array('isDelete' => 0);
    
    		if($options == null){
    			$result = $this->updateItem($params, $data);
    		}elseif($options['task'] == 'restore-select'){
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}elseif($options['task'] == 'restore-all'){
    			$params = $this->getFeatureDetail();
    
    			foreach($params as $key => $val){
    				$result = $this->updateItem($val, $data);
    			}
    		}
    		 
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }
    
    public function deleteItem($params = null, $options = null) {
    	try {
    		if($options == null){
    			$productRowset = $this->find($params);
    			$product = $productRowset->current();
    			$result = $product->delete();
    		}elseif($options['task'] == 'delete-select'){
    			foreach($params as $key => $val){
    				$productRowset = $this->find($params);
    				$product = $productRowset->current();
    				$result = $product->delete();
    			}
    		}elseif($options['task'] == 'delete-all'){
    			$params = $this->getProduct();
    
    			foreach($params as $key => $val){
    				$productRowset = $this->find($params);
    				$product = $productRowset->current();
    				$result = $product->delete();
    			}
    		}
    		 
    		return $result;
    	} catch (Exception $e) {
    		throw $e;
    	}
    }
    
    public function countItems() {
        $query = $this->select()
                ->from($this->_name, 'count(id) as total');
        $result = $this->fetchRow($query);
        if ($result) {
            return $result->total;
        } else {
            return null;
        }        
    }

}